package com.rimi.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.rimi.bean.User;
import com.rimi.cache.SelectCache;
import com.rimi.dao.UserDao;
import com.rimi.service.LoginService;
import com.rimi.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl extends ServiceImpl<UserDao, User> implements LoginService {

    @Autowired
    private SelectCache selectCache;

    @Autowired
    private UserService userService;

    @Override
    public User getUserByPhone(String phone) {
        //每次获取数据，需要判断缓存里面是否有数据
        User user =(User)selectCache.getCacheObj("user"+phone, User.class);
        if(user!=null){
            return user;
        }
        User user1 = userService.selectOne(new EntityWrapper<User>().eq("user_phone", phone));
        if(user1==null){
            return user1;
        }
        selectCache.addObjCache("user"+phone,user1);
        return user1;
    }

    @Override
    public User getUserByName(String name) {
        //每次获取数据，需要判断缓存里面是否有数据
        User user =(User)selectCache.getCacheObj("user"+name, User.class);
        if(user!=null){
            return user;
        }
        User user1 = userService.selectOne(new EntityWrapper<User>().eq("user_name", name));
        if(user1==null){
            return user1;
        }
        selectCache.addObjCache("user"+name,user1);
        return user1;
    }
}
